﻿package com.wandian.util
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.LocalConnection;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextFormat;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class CLZUtils extends Object
	{
		private static const ARIAL_FORMAT:TextFormat=new TextFormat("Arial");
		private static const ARIAL_BOLD_FORMAT:TextFormat=new TextFormat("Arial", null, null, true);

		public function CLZUtils()
		{
			return;
		} 

		public static function addElement(param1:Array, param2:Object):Boolean
		{
			if (param2 == null)
			{
				throw new Error("element is null");
			}
			if (param1.indexOf(param2) != -1)
			{
				return false;
			}
			param1.push(param2);
			return true;
		} 

		public static function removeElement(param1:Array, param2:Object):Boolean
		{
			var _loc_3:*=param1.indexOf(param2);
			if (_loc_3 == -1)
			{
				return false;
			}
			param1.splice(_loc_3, 1);
			return true;
		} 

		public static function getNoNullArray(param1:Array):Array
		{
			var array:*=param1;
			return array.filter(function(param1:Object, param2:int, param3:Array):Boolean
			{
				return param1 != null;
			} 
			);
		} 

		public static function getBitNumberString(param1:int, param2:int):String
		{
			var _loc_3:*=param1.toString();
			var _loc_4:*=_loc_3.length;
			while (_loc_4 < param2)
			{

				_loc_3="0" + _loc_3;
				_loc_4=_loc_4 + 1;
			}
			return _loc_3;
		} 

		public static function getHtmlColorString(param1:String, param2:uint):String
		{
			var _loc_3:*=param2.toString(16);
			return "<font color=\'#" + _loc_3 + "\'>" + param1 + "</font>";
		} 

		public static function getFileType(param1:String):String
		{
			var _loc_4:String=null;
			var _loc_2:String="";
			var _loc_3:*=param1.length - 1;
			while (_loc_3 >= 0)
			{

				_loc_4=param1.charAt(_loc_3);
				if (_loc_4 == ".")
				{
					break;
				}
				_loc_2=_loc_4 + _loc_2;
				_loc_3=_loc_3 - 1;
			}
			return _loc_2;
		} 

		public static function getFileName(param1:String):String
		{
			var _loc_5:String=null;
			var _loc_2:String="";
			var _loc_3:Boolean=false;
			var _loc_4:*=param1.length - 1;
			while (_loc_4 >= 0)
			{

				_loc_5=param1.charAt(_loc_4);
				if (_loc_3)
				{
					if (_loc_5 == "/")
					{
						break;
					}
					_loc_2=_loc_5 + _loc_2;
				}
				else if (_loc_5 == ".")
				{
					_loc_3=true;
				}
				_loc_4=_loc_4 - 1;
			}
			return _loc_2;
		} 

		public static function randSort(param1:int, param2:int):int
		{
			return Math.random() * 20 - 10;
		} 

		public static function formatArial(... args):void
		{
			var count:int=0;
			while (count < args.length)
			{

				args[count].defaultTextFormat=ARIAL_FORMAT;
				count++;
			}
			return;
		} 

		public static function formatBoldArial(... args):void
		{
			var count:int=0;
			while (count < args.length)
			{

				args[count].defaultTextFormat=ARIAL_BOLD_FORMAT;
				count++;
			}
			return;
		} 

		public static function openUrl(param1:String, param2:String="_blank", param3:String=""):void
		{
			var _loc_4:*=new URLRequest(param1);
			if (!ExternalInterface.available)
			{
				navigateToURL(_loc_4, param2);
				return;
			}
			var _loc_5:String="window.open";
			var _loc_6:*=ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
			if (_loc_6 != null)
			{
			}
			if (_loc_6.indexOf("Firefox") >= 0)
			{
				ExternalInterface.call(_loc_5, param1, param2, param3);
			}
			else
			{
				if (_loc_6 != null)
				{
				}
				if (_loc_6.indexOf("Safari") >= 0)
				{
					navigateToURL(_loc_4, param2);
				}
				else
				{
					if (_loc_6 != null)
					{
					}
					if (_loc_6.indexOf("MSIE") >= 0)
					{
						ExternalInterface.call(_loc_5, param1, param2, param3);
					}
					else
					{
						if (_loc_6 != null)
						{
						}
						if (_loc_6.indexOf("Opera") >= 0)
						{
							navigateToURL(_loc_4, param2);
						}
						else
						{
							navigateToURL(_loc_4, param2);
						}
					}
				}
			}
			return;
		} 

		public static function getBrowser():String
		{
			if (!ExternalInterface.available)
			{
				return "";
			}
			var _loc_1:*=ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
			return _loc_1;
		} 

		public static function getBrowserVersion():String
		{
			if (!ExternalInterface.available)
			{
				return "";
			}
			var _loc_1:*=getBrowser();
			if (_loc_1 != null)
			{
			}
			if (_loc_1.indexOf("Firefox") >= 0)
			{
				return "Firefox";
			}
			if (_loc_1 != null)
			{
			}
			if (_loc_1.indexOf("Chrome") >= 0)
			{
				return "Chrome";
			}
			if (_loc_1 != null)
			{
			}
			if (_loc_1.indexOf("Safari") >= 0)
			{
				return "Safari";
			}
			if (_loc_1 != null)
			{
			}
			if (_loc_1.indexOf("MSIE") >= 0)
			{
				if (_loc_1.indexOf("MSIE 8.0") >= 0)
				{
					return "MSIE 8.0";
				}
				if (_loc_1.indexOf("MSIE 6") >= 0)
				{
					return "MSIE 6.0";
				}
				if (_loc_1.indexOf("MSIE 9") >= 0)
				{
					return "MSIE 9.0";
				}
				if (_loc_1.indexOf("MSIE 7") >= 0)
				{
					return "MSIE 7.0";
				}
				return "MSIE";
			}
			else
			{
				if (_loc_1 != null)
				{
				}
				if (_loc_1.indexOf("Opera") >= 0)
				{
					return "Opera";
				}
				return "Unknow Browser";
			}
		} 

		public static function cacheAsBitmap(displayObj:DisplayObject):void
		{
			var i:int;
			if (displayObj is MovieClip)
			{
				if (displayObj["totalFrames"] > 1)
				{
					return;
				}
			}		
			if (displayObj is DisplayObjectContainer)
			{
				if ((displayObj as DisplayObjectContainer).numChildren != 0)
				{
					try
					{
						i;
						while (i < (displayObj as DisplayObjectContainer).numChildren)
						{

							cacheAsBitmap((displayObj as DisplayObjectContainer).getChildAt(i));
							i=(i + 1);
						}
					}
					catch (e:Error)
					{
					}
				}
			}
			else
			{
				displayObj.cacheAsBitmap=true;
			}
			return;
		} 

		public static function getRandomElement(param1:Array)
		{
			var _loc_2:*=int(Math.random() * param1.length);
			return param1[_loc_2];
		} 

		public static function addClickFunc(param1:IEventDispatcher, param2:Function):void
		{
			param1.addEventListener(MouseEvent.CLICK, param2);
			return;
		} 

		public static function removeClickFunc(param1:IEventDispatcher, param2:Function):void
		{
			param1.removeEventListener(MouseEvent.CLICK, param2);
			return;
		} 

		public static function gc():void
		{
			try
			{
				new LocalConnection().connect("CLZ");
				new LocalConnection().connect("CLZ");
			}
			catch (e:Error)
			{
			}
			return;
		} 

	}
}
